//
//  myActivityVC.swift
//  甜言蜜语
//
//  Created by yingli on 16/7/23.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit

class myActivityVC: UIViewController, myActivityBtnClicked,MyActivitySelectedDeleagte{
    @IBOutlet weak var tableView: UITableView!
    
    var modelList: [mySelfMyActivityInfo]?{
        didSet{
            //if modelList?.count != 0{
                self.initRightItem()
                
            //}
            self.tableView.reloadData()
        }
    }
    
    var type: String = "1"
    var myActivityView: MyActivitySelected?
    override func viewDidLoad() {
        super.viewDidLoad()
        
        publicMethod.hiddleExtendCellForTableView(self.tableView)
        
        self.getMyActivityInfo("1")
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.tabBarController?.tabBar.hidden = true
        self.navigationController?.navigationBar.barTintColor = NAVIGATIONBAR_RED_COLOR
        
    }
    //MARK: initRightItem
    func initRightItem(){
        let shareBtn = UIButton(frame: CGRectMake(0, 0, 25, 25))
        shareBtn.setBackgroundImage(UIImage(named: "right_item")!, forState: UIControlState.Normal)
        shareBtn.addTarget(self, action: #selector(self.shareAction(_:)), forControlEvents:  UIControlEvents.TouchUpInside)
        let share = UIBarButtonItem(customView: shareBtn)
        self.navigationItem.rightBarButtonItem = share
    }
    //右上角按钮点击事件
    //MARK:rightBtnTarget
    func shareAction(sender: UIButton){
        sender.selected = !sender.selected
        if sender.selected {
            myActivityView = MyActivitySelected.init(frame: kScreen_frame)
            myActivityView?.delegate = self
            myActivityView?.type = self.type
            self.view.addSubview(myActivityView!)
        } else {
            myActivityView?.closeBtnClick()
        }
       

        
    }
    
    //MARK:tableViewDelegate
    
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if(cell.respondsToSelector(Selector("setSeparatorInset:"))) {
            cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
        }
        if(cell.respondsToSelector(Selector("setLayoutMargins:"))) {
            cell.layoutMargins = UIEdgeInsetsMake(0, 0, 0, 0)
        }
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
           if let count = self.modelList?.count {
            return count
        }
        return 0
        
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        return 194
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("myActicityCell", forIndexPath: indexPath) as! myActicityCell
        let model = modelList![indexPath.row]
        
        cell.delegate = self
        cell.iv.sd_setImageWithURL(NSURL(string: model.imgsrc))
        cell.titleLbl.text = model.title
        cell.applyNumLbl.text = model.applynum
        cell.publishTimeLbl.text = model.publishtime
        cell.id = model.id
        
        return cell
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        
    }
    //MARK:MyActivitySelectedDeleagte
    func applyBtnClicked(type: String){
        self.type = type
        self.getMyActivityInfo(type)
    }
    //MARK:myActivityBtnClickedDelegate
    func acticityBtnClicked(id: Int){
        let sb = UIStoryboard(name: "Activity", bundle: nil)
        let vc = sb.instantiateViewControllerWithIdentifier("ActivityDetailViewController") as! ActivityDetailViewController
        vc.id = id
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    //MARK:networking
    func getMyActivityInfo(type:String){
      
        DataTool.mySelfMyActivity(type, completionHandler: { (newModel) in guard let newDataes = newModel else {
                return
            }
            print(newDataes)
            self.modelList = newDataes
        })
    }
}
